package fr.dauphine.spring.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fr.dauphine.spring.bo.Adresse;
import fr.dauphine.spring.bo.Client;
import fr.dauphine.spring.dao.AdresseDAO;
import fr.dauphine.spring.dao.ClientDAO;

@Component("dbFeeder")
public class DatabaseFeeder {
	private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
	@Autowired
	ClientDAO clientDAO;

	@Autowired
	AdresseDAO adresseDAO;
	// static String root =
	// "/Volumes/Space/Student-2010/pierha10/workspace-expert/Spring3MVC4/WebContent/";
	static String root = "C:/Users/Hadrien/Desktop/eclipse/Workspace/Spring3MVC4/WebContent/";

	public DatabaseFeeder() {
		try {
			feed();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void feed() throws Exception {

		String fichier = root + "resources/library.txt";

		try {
			InputStream ips = new FileInputStream(fichier);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;
			while ((ligne = br.readLine()) != null) {
				Client client = new Client();
				// s�paration client / adresse
				String tabClientAdresse[] = ligne.split("\\[");
				// r�cup�ration donn�es clients
				String tab[] = tabClientAdresse[0].split("\\|");
				client.setDateNaissance(formatDate(tab[0]));
				client.setNom(tab[1]);
				setImageFromFile(client, tab);
				client.setPrenom(tab[3]);
				// r�cup�ration adresse(s)
				ArrayList<Adresse> adressesClient = new ArrayList<Adresse>();
				String tabAdresse[] = tabClientAdresse[1].split(";");
				for (int i = 0; i < tabAdresse.length; i++) {
					Adresse adresse = new Adresse();
					String[] adresseSplit = tabAdresse[i].split("\\|");
					adresse.setClient(client);
					adresse.setEmail(adresseSplit[0]);
					adresse.setNumTel(adresseSplit[1]);
					adresse.setNumero(adresseSplit[2]);
					adresse.setPays(adresseSplit[3]);
					adresse.setPostCode(adresseSplit[4]);
					adresse.setRue(adresseSplit[5]);
					adresse.setType(adresseSplit[6]);
					adresse.setVille(adresseSplit[7]);

					System.out.println("Adresse : " + adresse);
					System.out.println(adresseDAO);
					System.out.println(clientDAO);
					adresseDAO.save(adresse);
					adressesClient.add(adresse);
				}
				client.setAdresses(adressesClient);
				clientDAO.save(client);
				System.out.println(client);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ClientDAO getClientDAO() {
		return clientDAO;
	}

	public void setClientDAO(ClientDAO clientDAO) {
		this.clientDAO = clientDAO;
	}

	public AdresseDAO getAdresseDAO() {
		return adresseDAO;
	}

	public void setAdresseDAO(AdresseDAO adresseDAO) {
		this.adresseDAO = adresseDAO;
	}

	private static void setImageFromFile(Client client, String[] tab) throws FileNotFoundException,
			IOException {
		File f = new File(root + "resources/images/" + tab[2]);
		byte[] photo = new byte[(int) (f.length())];
		FileInputStream fi = new FileInputStream(f);
		fi.read(photo);
		client.setPhoto(photo);
	}

	public static Date formatDate(String date) {
		try {
			return formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

}